package org.cloudie.docs.editor.client.content.dialogs;

import java.util.Date;

import org.cloudie.docs.editor.client.commands.generic.BrowserOpenCommand;
import org.cloudie.docs.editor.client.commands.generic.VirtualEntryAddCommand;
import org.cloudie.docs.editor.client.events.CommandEvent;
import org.cloudie.docs.editor.client.widgets.ImageBox;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

/**
 * A dialog window containing a new Icon form.
 */
public class NewIconDialog extends FormDialog {

  protected static NewIconDialog instance;
  
  public static NewIconDialog get() {
    if (instance == null) {
      instance = new NewIconDialog();
    }
    return instance;
  }

  private static final String[] IMAGE_IDS = new String[] {
	"accessories_dictionary",
	"akregator",
	"applications_accessories",
	"applications_education",
	"applications_engineering",
	"applications_games",
	"applications_graphics",
	"applications_multimedia",
	"applications_science",
	"applications_toys",
	"cup",
	"hardware",
	"internet_telephony",
	"kchart",
	"kgeography",
	"kfind",
	"kmplot",
	"kpercentage",
	"ktip",
	"kteatime",
	"marble",
	"kstars",
	"personal",
	"preferences_desktop_notification_bell",
	"preferences_system_session_services",
	"todolist",
	"system_file_manager",
	"strigi",
  };
  private TextBox iconLabel, iconAction;
  private TextArea iconDescription;
  private ImageBox iconImage;
  
  /**
   * Constructs a New Icon dialog.
   */
  protected NewIconDialog() {
    super("New Desktop Icon", true);
    addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        hide();
      }
    });
    buildForm();
  }
  
  /**
   * Builds the New Icon form.
   */
  @Override
  protected void buildForm() {
    iconLabel = new TextBox();
    iconLabel.setWidth("300px");
    iconAction = new TextBox();
    iconAction.setWidth("300px");
    iconDescription = new TextArea();
    iconDescription.setSize("300px", "80px");
    iconImage = new ImageBox("/images/icons/large/apps/%1.png", "application_x_mswinurl", IMAGE_IDS);
    iconImage.setPixelSize(50, 50);
    submit.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
          if (iconLabel.getValue().equals("")) {
        	Window.alert("Label is a required field.");
        	return;
          }
          VirtualEntryAddCommand cmd = new VirtualEntryAddCommand(
        		  null, iconLabel.getValue(), "apps/" + iconImage.getValue(), iconDescription.getValue(), null, 0, 0, -1,
        		  new BrowserOpenCommand(iconAction.getValue(), String.valueOf(new Date().getTime())));
          cmd.setDistribute(true);
          CommandEvent.fire(cmd);
          hide();
      }
    });
    cancel.addClickHandler(new ClickHandler(){
        public void onClick(ClickEvent event) {
          hide();
        }
    });
    addField(iconLabel, "Label:");
    addField(iconDescription, "Description:");
    addField(iconAction, "Shortcut URL");
    addField(iconImage, "Image");
    super.buildForm();
  }
  
  @Override
  public void hide() {
    super.hide();
    resetForm();
  }
  
  @Override
  public void resetForm() {
    iconLabel.setValue("");
    iconDescription.setValue("");
    iconAction.setValue("");
    iconImage.setValue(null);
  }
}
